﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model;
using HengTong.Model.Db.Other;


namespace HengTong.ViewModel.Activity.SysActivityListVms
{
    public partial class SysActivityListListVm : BasePagedListVM<SysActivityListView, SysActivityListSearcher>
    {

        protected override IEnumerable<IGridColumn<SysActivityListView>> InitGridHeader()
        {
            return new List<GridColumn<SysActivityListView>>{
                this.MakeGridHeader(x => x.ActivityName),
                this.MakeGridHeader(x => x.StartTime),
                this.MakeGridHeader(x => x.EndTime),
                this.MakeGridHeader(x => x.ActivityStatus),
                this.MakeGridHeader(x => x.MaxJoinCount),
                this.MakeGridHeader(x => x.ParticipationCount),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysActivityListView> GetSearchQuery()
        {//获取当前登录用户Id
            Searcher.UserId =  Guid.Parse(Wtm.LoginUserInfo.UserId);
            var query = DC.Set<SysActivityList>()
                .CheckContain(Searcher.ActivityName, x=>x.ActivityName)
                .CheckBetween(Searcher.StartTime?.GetStartTime(), Searcher.StartTime?.GetEndTime(), x => x.StartTime, includeMax: false)
                .CheckBetween(Searcher.EndTime?.GetStartTime(), Searcher.EndTime?.GetEndTime(), x => x.EndTime, includeMax: false)
                .CheckEqual(Searcher.ActivityStatus, x=>x.ActivityStatus)
                .CheckEqual(Searcher.MaxJoinCount, x=>x.MaxJoinCount)
                .Select(x => new SysActivityListView
                {
				    ID = x.ID,
                    ActivityName = x.ActivityName,
                    StartTime = x.StartTime,
                    EndTime = x.EndTime,
                    ActivityStatus = x.ActivityStatus,
                    ParticipationCount = DC.Set<SysActivityParticipation>().Count(e => e.ActivityId == x.ID && e.UserId == Searcher.UserId),
                    MaxJoinCount = x.MaxJoinCount,
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysActivityListView : SysActivityList{
        [Display(Name = "活动参与次数")]
        public int ParticipationCount { get; set; }

    }
}
